﻿using IDEAMS.model;
using IDEAMS.model.alarm;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;


namespace IDEAMS.view.alarm
{
    /// <summary>
    /// AlarmRecordDlg.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmRecordDlg : Window
    {
        private AlarmRecordDlgViewModel windowViewModel;
        public AlarmRecordDlg()
        {
            InitializeComponent();
        }

        public AlarmRecordDlg(string serialNo) : this()
        {
            windowViewModel = new AlarmRecordDlgViewModel(serialNo);
            this.DataContext = windowViewModel;
        }
    }

    public class AlarmRecordDlgViewModel : ViewModelBase
    {
        public AlarmRecordDlgViewModel(string serialNo)
        {
            AlarmInfoCollection alarmInfoCollection = new AlarmInfoCollection();
            alarmInfoCollection.Load(serialNo);

            alarmInfos = new ObservableCollection<AlarmInfoViewModel>();
            alarmInfoCollection.ForEach(x =>
            {
                AlarmInfoViewModel info = new AlarmInfoViewModel(x);
                alarmInfos.Add(info);
                return false;
            });

            //排序
            var sortedList = alarmInfos.OrderByDescending(item => item.endTime).ToList();

            alarmInfos.Clear();
            foreach(var item in sortedList)
            {
                alarmInfos.Add(item);
            }

            title = "报警记录-"+serialNo;
        }

        private string m_title;
        public string title
        {
            get { return m_title; }
            set
            {
                m_title = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<AlarmInfoViewModel> m_alarmInfos;
        public ObservableCollection<AlarmInfoViewModel> alarmInfos
        {
            get { return m_alarmInfos; }
            set
            {
                m_alarmInfos = value;
                NotifyPropertyChanged();
            }
        }
    }
}
